package com.lightsh.object;

import org.jbox2d.common.Vec2;

import android.graphics.Canvas;
import android.graphics.Rect;

import com.lightsh.gui.BaseScreen;
import com.lightsh.utility.GeometryUtility;

public abstract class RectangleObject extends PolygonObject{

	protected float mHalfW;
	protected float mHalfH;
	public RectangleObject(BaseScreen screen, float x, float y, float halfWidth, float halfHeight) {
		super(screen, x, y);
		mHalfW = halfWidth;
		mHalfH = halfHeight;

		add(new Vec2(-halfWidth, -halfHeight));
		add(new Vec2(halfWidth, -halfHeight));
		add(new Vec2(halfWidth, halfHeight));
		add(new Vec2(-halfWidth, halfHeight));
		
	}

	@Override
	public void draw(Canvas canvas, Rect screenRect, float selfScale,
			float worldScale) {
		super.draw(canvas,screenRect, selfScale, worldScale);		
		if(!isInRect(screenRect, selfScale)){
			return;
		}		
		Vec2 vec2 = getPosition();
		canvas.save();
		rotateCanvas(canvas, mAngle, vec2, screenRect, selfScale, worldScale);
		if(mSprite != null){
			mSprite.draw(canvas, mPaint, (int)((vec2.x - mHalfW) / selfScale + mDrawImageOffsetX - screenRect.left), (int)((vec2.y - mHalfH) / selfScale + mDrawImageOffsetY - screenRect.top), worldScale);
		}else if(mBitmap != null){
			drawBitmap(canvas, vec2.x - mHalfW, vec2.y - mHalfH, screenRect, selfScale, worldScale);
		}else{
			GeometryUtility.trackPolygonPath(mPath, vec2, mPolygonVertexs, mVertices, selfScale, worldScale, screenRect.left, screenRect.top);
			canvas.drawPath(mPath, mPaint);
		}		
		canvas.restore();
	}



	@Override
	public boolean isInRect(Rect rect, float selfScale) {
		Vec2 centerPoint = getPosition();
		return GeometryUtility.isInRectCenter(centerPoint, mHalfW, mHalfH, rect, selfScale);
	}

	@Override
	public boolean isPointInside(Vec2 point, float worldScale, float screenX, float screenY) {
		return GeometryUtility.isPointInRectangle(getPosition(), worldScale, screenX, screenY, mHalfW, mHalfH, point);
	}

}
